package com.course.web.component.base;

import com.vaadin.ui.*;

import java.util.Comparator;
import java.util.List;

/**
 * Created by Justin on 2017/6/10.
 */
public class EditGrid<T> extends CustomComponent {
    private final BaseGrid<T> grid;
    private final BaseEdit<T> edit;
    private final BaseEdit<T> drop;

    public EditGrid(Class<T> type) {
        this.grid = new BaseGrid<>();
        this.edit = new BaseEdit<>(type);
        this.drop = new BaseEdit<>(type);
        initGrid(grid);
        initEdit(edit);
        initDrop(drop);
        initRoot();
    }

    public void init(List<T> items) {
        grid.setItems(items);
    }

    public void create(T item) {
        item = doCreate(item);
        grid.appendItem(item);
        doSort();
    }

    public void update(T item) {
        item = doUpdate(item);
        grid.updateItem(item);
        doSort();
    }

    public void delete(T item) {
        item = doDelete(item);
        grid.deleteItem(item);
        doSort();
    }

    public void insert(T item, int index) {
        item = doInsert(item, index);
        grid.insertItem(item, index);
        doSort();
    }

    public void sort(Comparator<T> comparator) {
        grid.sortItems(comparator);
    }

    public void createShow() {
        T item = doCreate();
        doCreateShow(edit);
        edit.setHandler(this::create);
        edit.show(getUI(), item);
    }

    public void updateShow(T item) {
        doUpdateShow(edit);
        edit.setHandler(this::update);
        edit.show(getUI(), item);
    }

    public void deleteShow(T item) {
        doDeleteShow(drop);
        drop.setHandler(this::delete);
        drop.show(getUI(), item);
    }

    public List<T> getItems() {
        return grid.getItems();
    }

    private void initRoot() {
        Layout root = new VerticalLayout();
        Layout menu = new HorizontalLayout();
        TextField text = new TextField();

        root.addComponent(menu);
        root.addComponent(grid);
        menu.addComponent(text);

        doInitMenu(menu);

        setCompositionRoot(root);
        setSizeFull();

        text.setEnabled(false);
        text.setPlaceholder("搜索");
    }

    private void initGrid(BaseGrid<T> grid) {
        doInitGrid(grid);
    }

    private void initEdit(BaseEdit<T> edit) {
        doInitEdit(edit);
    }

    private void initDrop(BaseEdit<T> drop) {
        doInitDrop(drop);
    }

    protected void doInitMenu(Layout menu) {

    }

    protected void doInitGrid(BaseGrid<T> grid) {
    }

    protected void doInitEdit(BaseEdit<T> edit) {
    }

    protected void doInitDrop(BaseEdit<T> drop) {
        drop.initEdit(new Label("确定删除？"));
    }

    protected void doCreateShow(BaseEdit<T> edit) {
        edit.setCaption("新增");
    }

    protected void doUpdateShow(BaseEdit<T> edit) {
        edit.setCaption("编辑");
    }

    protected void doDeleteShow(BaseEdit<T> drop) {
        drop.setCaption("删除");
    }

    protected T doCreate() {
        return null;
    }

    protected T doCreate(T item) {
        return item;
    }

    protected T doUpdate(T item) {
        return item;
    }

    protected T doDelete(T item) {
        return item;
    }

    protected T doInsert(T item, int index) {
        return item;
    }

    protected void doSort() {

    }
}
